import {delay} from 'redux-saga';
import {
  takeEvery,
  put,
  call,
  all,
  select,
  fork,
  take
} from 'redux-saga/effects';
import {LOGIN_REQUEST, LOGIN_SUCCESS, LOGIN_FAILURE} from 'actions';
import ajax from 'ajax';

export default function * watchLogin() {
  while (true) {
    const payload = yield take(LOGIN_REQUEST);
    try {
      const response = yield call(ajax.get, 'user', {
        params: {
          username: payload.username,
          password: payload.password
        }
      });
      if (response.status === 200 && response.data && response.data !== '') {
        yield put({type: LOGIN_SUCCESS, data: response.data});
      } else {
        yield put({type: LOGIN_FAILURE, error: '未找到用户,请先注册！'});
      }
    } catch (error) {
      yield put({type: LOGIN_FAILURE, error: error.message});
    }
  }
}
